#!/bin/sh
restart_tomcat()
{
    echo "第一步=======================停止tomcat服务========================================================================"
	echo "当前的路径为:"
	pwd
	echo "正准备停止当前正在运行的wg_bsp服务.............."
	if [ $tomcatNum  -eq 5 ]; then
		ps -ef|grep "/home/Enfry-job/Enfry-job-0.0.1-SNAPSHOT.jar"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
		echo "======== 已经停止当前正在运行的tomcat服务========"
		rm -rf /home/Enfry-job/nohup.out
		echo "第二步========================重启tomcat======================================================================="
		cd /home/Enfry-job/
		nohup java -jar ./Enfry-job-0.0.1-SNAPSHOT.jar &  
	elif [ $tomcatNum  -eq 6 ]; then
		ps -ef|grep "/home/Gateway-job/GateWay-job-0.0.1-SNAPSHOT.jar"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
		echo "======== 已经停止当前正在运行的tomcat服务========"
		rm -rf /home/Gateway-job/nohup.out
		echo "第二步========================重启tomcat======================================================================="
		cd /home/Gateway-job/
		nohup java -jar ./GateWay-job-0.0.1-SNAPSHOT.jar &  
	else
		ps -ef|grep "/home/enfry/tomcat_$tomcatNum/"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
		echo "======== 已经停止当前正在运行的tomcat服务========"
		rm -rf /home/enfry/tomcat_$tomcatNum/work/*
		rm -rf /home/enfry/tomcat_$tomcatNum/logs/*
		rm -rf /home/logs/enfry-log/*
		rm -rf /home/logs/gateway-log/*
		echo "第二步========================重启tomcat======================================================================="
		cd /home/enfry/tomcat_$tomcatNum/bin
		./startup.sh
	fi
	
	if [ $? -eq 0 ];then
	echo "tomcat_$tomcatNum启动成功"
	else
	echo "========tomcat_$tomcatNum启动失败...... ========"
	fi
}
restart_all_tomcat()
{
	echo "停止多个tomcat开始.............."
	pwd
	ps -ef|grep "/home/enfry/tomcat_1/"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	ps -ef|grep "/home/enfry/tomcat_2/"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	ps -ef|grep "/home/enfry/tomcat_3/"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	ps -ef|grep "/home/enfry/tomcat_4/"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	ps -ef|grep "/home/Enfry-job/Enfry-job-0.0.1-SNAPSHOT.jar'"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	ps -ef|grep "/home/Gateway-job/GateWay-job-0.0.1-SNAPSHOT.jar'"|grep -v "grep"|awk '{print $2}'|xargs -i kill -9 {}
	echo "tomcat 停止成功"


	echo "所有tomcat停止结束............................."
	rm -rf /home/enfry/tomcat_1/work/*
	rm -rf /home/enfry/tomcat_2/work/*
	rm -rf /home/enfry/tomcat_3/work/*
	rm -rf /home/enfry/tomcat_4/work/*
	rm -rf /home/enfry/tomcat_1/logs/*
	rm -rf /home/enfry/tomcat_2/logs/*
	rm -rf /home/enfry/tomcat_3/logs/*
	rm -rf /home/enfry/tomcat_4/logs/*
	rm -rf /home/logs/enfry-log/*
	rm -rf /home/logs/gateway-log/*
	rm -rf /home/Enfry-job/nohup.out
	rm -rf /home/Gateway-job/nohup.out

	echo "启动多个tomcat.............."
	cd /home/enfry/tomcat_1/bin
	./startup.sh
	if [ $? -eq 0 ];then
	echo "tomcat1启动成功"
	else
	echo "========tomcat1启动失败...... ========"
	fi

	cd /home/enfry/tomcat_2/bin
	./startup.sh
	if [ $? -eq 0 ];then
	echo "tomcat2启动成功"
	else
	echo "========tomcat2启动失败...... ========"
	fi

	cd /home/enfry/tomcat_3/bin
	./startup.sh
	if [ $? -eq 0 ];then
	echo "tomcat3启动成功"
	else
	echo "========tomcat3启动失败...... ========"
	fi

	cd /home/enfry/tomcat_4/bin
	./startup.sh
	if [ $? -eq 0 ];then
	echo "tomcat4启动成功"
	else
	echo "========tomcat4启动失败...... ========"
	fi
	
	cd /home/Enfry-job/
	nohup java -jar ./Enfry-job-0.0.1-SNAPSHOT.jar &  
	if [ $? -eq 0 ];then
	echo "EN+Job启动成功"
	else
	echo "========EN+Job启动失败...... ========"
	fi
	
	cd /home/Gateway-job/
	nohup java -jar ./GateWay-job-0.0.1-SNAPSHOT.jar &  
	if [ $? -eq 0 ];then
	echo "GateWayJob启动成功"
	else
	echo "========GateWayJob启动失败...... ========"
	fi
	
}


echo "首先=======================选择重启的tomcat========================================================================"
echo "请按照如下提示选择要重启的tomcat："
echo "0:表示全部重启:EN+,GATEWAY,EN+Job,GATEWAYJob"
echo "1:表示tomcat_1：EN+第一台"
echo "2:表示tomcat_2：GATEWAY"
echo "3:表示tomcat_3：EN+第二台"
echo "4:表示tomcat_4：GATEWAY第二台"
echo "5:表示：EN+Job"
echo "6:表示：GATEWAYJob"
read -p "请输入本次要重启的服务器:" tomcatNum
if [ ! $tomcatNum ]; then 
echo "输入错误，请重新执行"
else
	if [ $tomcatNum  -eq 0 ]; then
		echo "当前输入的重启服务器为：全部重启"
		restart_all_tomcat
	else
		echo "当前输入的重启服务器为：tomcat_$tomcatNum"
		restart_tomcat
	fi
fi